package com.bwie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bwie.mapper.*;
import com.bwie.pojo.*;
import com.bwie.service.ProdService;
import com.bwie.utils.Result;
import com.bwie.vo.PageInfoVo;
import com.bwie.vo.ProdVo;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.Date;
import java.util.List;

/**
* @author 陈彤彤
* @description 针对表【t_prod(商品表)】的数据库操作Service实现
* @createDate 2025-07-22 09:59:54
*/
@Service
public class ProdServiceImpl extends ServiceImpl<ProdMapper, Prod>
    implements ProdService{

    @Autowired
    ProdMapper prodMapper;

    @Autowired
    ProdAttaMapper prodAttaMapper;

    @Autowired
    DictMapper dictMapper;

    @Autowired
    BrandMapper brandMapper;

    @Autowired
    UnitMapper unitMapper;

    @Autowired
    TagMapper tagMapper;

    @Autowired
    DealerMapper dealerMapper;

    @Autowired
    TypeMapper typeMapper;

    @Autowired
    ProdInfoMapper prodInfoMapper;

    /**
    * @param prodVo:
    * @return Result
    * @author :陈彤彤
    * @description 商品分页列表
    * @date 2025/7/22 10:29
    */
    @Override
    public Result prodList(ProdVo prodVo) {
        /*构造分页参数*/
        Page<Prod> prodPage = new Page<>(prodVo.getPageNum(), prodVo.getPageSize());

        Page<Prod> page = prodMapper.prodList(prodPage, prodVo);
        return Result.ok().setData(page);
    }

    /**
    * @param ids:
    * @return Result
    * @author :陈彤彤
    * @description 批量删除商品信息
    * @date 2025/7/22 16:26
    */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result deleteBath(List<Integer> ids) {
        //批量删除商品
        prodMapper.deleteBatchIds(ids);

        //批量删除商品附件
        prodAttaMapper.delete(new QueryWrapper<ProdAtta>().lambda().in(ProdAtta::getProdId,ids));

        return Result.ok();
    }

    /**
    * @param prodAtta:
    * @return Result
    * @author :陈彤彤
    * @description 添加商品附件信息
    * @date 2025/7/22 20:10
    */
    @Override
    public Result addProdAtta(ProdAtta prodAtta) {
        /*判断商品是否存在*/
        Prod prod = prodMapper.selectById(prodAtta.getProdId());
        if (prod == null){
            return Result.error().setMsg("商品不存在");
        }

        /*删除原有附件*/
        prodAttaMapper.delete(new QueryWrapper<ProdAtta>().lambda().eq(ProdAtta::getAttaId,prodAtta.getProdId()));

        /*添加附件*/
        prodAttaMapper.insert(prodAtta);
        return Result.ok();
    }

    /**
    * @param typeId:
    * @return Result
    * @author :陈彤彤
    * @description 字典表信息查询
    * @date 2025/7/22 21:25
    */
    @Override
    public Result dictList(Integer typeId) {
        List<Dict> dictList = dictMapper.selectList(new QueryWrapper<Dict>().lambda().eq(Dict::getDictType, typeId));
        return Result.ok().setData(dictList);
    }

    /**
    * @param :
    * @return Result
    * @author :陈彤彤
    * @description TODO
    * @date 2025/7/22 21:35
    */
    @Override
    public Result selectBrand() {
        List<Brand> brandList = brandMapper.selectList(null);
        return Result.ok().setData(brandList);
    }

    /**
    * @param :
    * @return Result
    * @author :陈彤彤
    * @description TODO
    * @date 2025/7/22 21:39
    */
    @Override
    public Result selectUnit() {
        List<Unit> unitList = unitMapper.selectList(null);
        return Result.ok().setData(unitList);
    }

    /**
    * @param :
    * @return Result
    * @author :陈彤彤
    * @description TODO
    * @date 2025/7/22 21:39
    */
    @Override
    public Result selectTag() {
        List<Tag> tagList = tagMapper.selectList(null);
        return Result.ok().setData(tagList);
    }

    /**
    * @param :
    * @return Result
    * @author :陈彤彤
    * @description TODO
    * @date 2025/7/22 21:40
    */
    @Override
    public Result selectDealer() {
        List<Dealer> dealerList = dealerMapper.selectList(null);
        return Result.ok().setData(dealerList);
    }

    /**
    * @param :
    * @return Result
    * @author :陈彤彤
    * @description 查询类型树信息
    * @date 2025/7/23 9:39
    */
    @Override
    public Result selectTypeTree() {
        List<Type> typeList = typeMapper.selectList(null);
        return Result.ok().setData(typeList);
    }

    /**
    * @param prod:
    * @return Result
    * @author :陈彤彤
    * @description 添加商品信息
    * @date 2025/7/23 10:23
    */
    @Override
    public Result addProd(Prod prod) {
        Prod selectOne = prodMapper.selectOne(new QueryWrapper<Prod>().lambda().eq(Prod::getProdName, prod.getProdName()));
        if (selectOne != null){
            return Result.error().setMsg("商品信息已存在");
        }
        prodMapper.insert(prod);
        return Result.ok();
    }

    /**
    * @param pageInfoVo:
    * @return Result
    * @author :陈彤彤
    * @description 商品明细分页列表
    * @date 2025/7/25 20:04
    */
    @Override
    public Result prodInfoList(PageInfoVo pageInfoVo) {
        /*构造分页参数*/
        Page<ProdInfo> prodInfoPage = new Page<>(pageInfoVo.getPageNum(), pageInfoVo.getPageSize());

        QueryWrapper<ProdInfo> wrapper = new QueryWrapper<>();
        wrapper.lambda().orderByDesc(ProdInfo::getInfoId);

        Page<ProdInfo> page = prodInfoMapper.selectPage(prodInfoPage, wrapper);
        return Result.ok().setData(page);
    }

    /**
    * @param prodInfo:
    * @return Result
    * @author :陈彤彤
    * @description 添加商品明细
    * @date 2025/7/25 20:55
    */
    @Override
    public Result addProdInfo(ProdInfo prodInfo) {
        prodInfo.setCreateBy("system");
        prodInfo.setCreateTime(new Date());
        prodInfo.setUpdateBy("system");
        prodInfo.setUpdateTime(new Date());
        prodInfoMapper.insert(prodInfo);
        return Result.ok();
    }

}




